package com.itita.ww2.handler.requesthandler.army;

import java.sql.SQLException;

import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.game.user.UserInventoryManager;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.handler.IRequestOper;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

/**
 * 士兵穿上装备
 * @author NorthLan
 *
 */
public class Equip implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		String grid = params.getUtfString("grid"); // 装备格子(装备到哪个格子)
		String uuid = params.getUtfString("uuid"); // 物品(装备)uuid
		String armyId = params.getUtfString("armyId"); // 士兵id

		UserInventoryManager inventory = userProfile.getInventory();
		inventory.backup();
		try {
			// 穿装
			userProfile.getArmyManager().armyEquip(armyId, grid, uuid);
		} catch (Exception e) {
			inventory.rollback();
			throw e;
		} finally {
			inventory.clean();
		}

		inventory.update();

		ISFSObject backObj = SFSObject.newInstance();
		backObj.putSFSArray("inventory", inventory.toSFSArray());
		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
